; cstring.asm - Rotinas p/ manipular strings no formato de strings
; do C (vetor de caracteres finalizado com '\0' ou ascii(0))
; Copyright (C) 2008 Italo Valcy (italo@dcc.ufba.br)
; 
; This program is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation; either version 2 of the License, or
; (at your option) any later version.
; 
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; GNU General Public License for more details.
; 
; You should have received a copy of the GNU General Public License along
; with this program; if not, write to the Free Software Foundation, Inc.,
; 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


section .text
   global   _strcmp
   global   _strlen

; int strcmp ( const char * str1, const char * str2 );
_strcmp:

;+----------------------------------------------------------------------+
;| Chamada:    xcall _strcmp,str1,str2                                  |
;|                                                                      |
;|    str1     db    'valor da primeira string',0                       |
;|    str2     db    'valor da segunda string',0                        |
;|                                                                      |
;| Returno:                                                             |
;|                                                                      |
;|    eax == 0 --> As strings sao iguais                                |
;|    eax > 0  --> O primeiro caracter diferente entre                  |
;|                 elas, tem valor maior em str1.                       |
;|    eax < 0  --> Caso contrario.                                      |
;+----------------------------------------------------------------------+
   push  ebp
   mov   ebp, esp

   mov   esi, dword [ebp+8]
   mov   edi, dword [ebp+12]
   cld

.WhileEquals:
   cmp   byte [esi], 0
   je    .Return
   cmp   byte [edi], 0
   je    .Return
   cmpsb
   je    .WhileEquals
   dec   esi
   dec   edi

.Return:
   movzx eax, byte [esi]
   movzx ebx, byte [edi]
   sub   eax, ebx
   leave
   ret


; size_t strlen ( const char * str );
_strlen:

;+----------------------------------------------------------------------+
;| Chamada:    xcall _strlen,str                                        |
;|                                                                      |
;|    str      db    'valor da string',0                                |
;|                                                                      |
;| Returno:                                                             |
;|                                                                      |
;|    eax  --> O tamanho da string                                      |
;+----------------------------------------------------------------------+
   push  ebp
   mov   ebp, esp

   mov   esi, dword [ebp+8]
   mov   eax, -1
.NextChar:
   inc   eax
   cmp   byte [esi+eax], 0
   jne   .NextChar

   leave
   ret
